<template>
    <el-button @click="openDialog">openDialog</el-button>
    <div id="wrap">
        <div class="p-[10px]">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
        <div class="item">item</div>
    </div>
</template>

<script setup>
// import { print } from 'npm-package-mey'
// print()

const openDialog = () => {
    const title = '五金', contentId = 'wrap'
    createDialog({ title, contentId })
}

const createDialog = (options) => {
    const { title, contentId, closeOnClickModal = true } = options
    const el = document.querySelector('#' + contentId)
    const elCopy = el.cloneNode(true)
    elCopy.style.display = 'block'
    // 定义 HTML 字符串
    const htmlString = `
    <div class="mask-layer">
        <div class="custom-dialog">
            <div class="custom-dialog-header">
                <span class="custom-dialog-title">${title || '弹窗标题'}</span>
                <button class="custom-dialog-button">x</button>
            </div>
            <div class="custom-dialog-body">
                ${elCopy.outerHTML}
            </div>
        </div>
    </div>
    `;

    // 将 HTML 字符串插入到 body 或其他容器中
    document.body.insertAdjacentHTML('beforeend', htmlString);

    const maskLayerEl = document.querySelector('.mask-layer');
    const dialogEl = document.querySelector('.custom-dialog');

    // 显示弹窗
    setTimeout(() => {
        maskLayerEl.classList.add('show');
        dialogEl.classList.add('show');
    });

    // 关闭弹窗函数
    const closeDialog = () => {
        maskLayerEl.classList.remove('show');
        dialogEl.classList.remove('show');
        setTimeout(() => {
            maskLayerEl.remove();
        }, 300); // 时间应与 CSS 动画时间匹配
    };

    // 点击按钮关闭弹窗
    const closeBtnEl = document.querySelector('.custom-dialog-button');
    closeBtnEl.onclick = closeDialog

    // 点击遮罩层关闭弹窗
    maskLayerEl.onclick = (event) => {
        if (closeOnClickModal && event.target === maskLayerEl) { // 确保点击的是遮罩层本身
            closeDialog();
        }
    };
}
</script>

<style lang="scss">
.mask-layer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 2000;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.502);
    overflow: auto;
    opacity: 0;
    transition: opacity 0.3s ease;
    .custom-dialog {
        position: relative;
        margin: 50px auto;
        background: #fff;
        border-radius: 4px;
        box-shadow: 0px 12px 32px 4px rgba(0, 0, 0, 0.04),
            0px 8px 20px rgba(0, 0, 0, 0.08);
        box-sizing: border-box;
        padding: 16px;
        width: 50%;
        overflow-wrap: break-word;

        /* 初始状态 */
        opacity: 0;
        transform: scale(0.9);
        transition: opacity 0.3s ease, transform 0.3s ease;

        .custom-dialog-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 10px;
            .custom-dialog-title {
                font-size: 18px;
            }
            .custom-dialog-button {
                position: absolute;
                top: 0;
                right: 0;
                padding: 0;
                width: 48px;
                height: 48px;
                background: transparent;
                border: none;
                outline: none;
                cursor: pointer;
                font-size: 18px;
                color: #909399;
            }
        }
    }
    /* 动画状态 */
    .custom-dialog.show {
        opacity: 1;
        transform: scale(1);
    }
}

.mask-layer.show {
    opacity: 1;
}
#wrap {
    background: #eee;
    display: none;
    .item {
        height: 30px;
        width: 100%;
        background: #ccc;
    }
}
</style>